package net.jgrm.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.jgrm.order.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 订单Mapper接口
 * 支持业务层加密
 */
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 插入订单（业务层加密）
     */
    @Insert({
            "INSERT INTO t_order (",
            "user_id, order_no, total_amount, status,",
            "receiver_name, receiver_phone, receiver_phone_prefix, receiver_phone_suffix, receiver_name_hash, receiver_phone_hash,",
            "receiver_province, receiver_city, receiver_district, receiver_address,",
            "create_time, update_time",
            ") VALUES (",
            "#{order.userId}, #{order.orderNo}, #{order.totalAmount}, #{order.status},",
            "#{order.receiverName}, #{order.receiverPhone}, #{order.receiverPhonePrefix}, #{order.receiverPhoneSuffix}, #{order.receiverNameHash}, #{order.receiverPhoneHash},",
            "#{order.receiverProvince}, #{order.receiverCity}, #{order.receiverDistrict}, #{order.receiverAddress},",
            "#{order.createTime}, #{order.updateTime}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "order.id")
    int insertOrderWithEncryption(@Param("order") Order order);

    /**
     * 根据姓名哈希查询订单
     */
    @Select({
            "SELECT id, user_id, order_no, total_amount, status,",
            "receiver_name, receiver_phone,",
            "receiver_name_hash, receiver_phone_hash,",
            "receiver_phone_prefix, receiver_phone_suffix,",
            "receiver_province, receiver_city, receiver_district, receiver_address,",
            "create_time, update_time",
            "FROM t_order",
            "WHERE receiver_name_hash = #{nameHash}"
    })
    List<Order> findByReceiverNameHash(@Param("nameHash") String nameHash);

    /**
     * 根据手机号哈希查询订单
     */
    @Select({
            "SELECT id, user_id, order_no, total_amount, status,",
            "receiver_name, receiver_phone,",
            "receiver_name_hash, receiver_phone_hash,",
            "receiver_phone_prefix, receiver_phone_suffix,",
            "receiver_province, receiver_city, receiver_district, receiver_address,",
            "create_time, update_time",
            "FROM t_order",
            "WHERE receiver_phone_hash = #{phoneHash}"
    })
    List<Order> findByReceiverPhoneHash(@Param("phoneHash") String phoneHash);

    /**
     * 根据手机号前缀查询订单
     */
    @Select({
            "SELECT id, user_id, order_no, total_amount, status,",
            "receiver_name, receiver_phone,",
            "receiver_name_hash, receiver_phone_hash,",
            "receiver_phone_prefix, receiver_phone_suffix,",
            "receiver_province, receiver_city, receiver_district, receiver_address,",
            "create_time, update_time",
            "FROM t_order",
            "WHERE receiver_phone_prefix = #{phonePrefix}"
    })
    List<Order> findByReceiverPhonePrefix(@Param("phonePrefix") String phonePrefix);

    /**
     * 根据手机号后缀查询订单
     */
    @Select({
            "SELECT id, user_id, order_no, total_amount, status,",
            "receiver_name, receiver_phone,",
            "receiver_name_hash, receiver_phone_hash,",
            "receiver_phone_prefix, receiver_phone_suffix,",
            "receiver_province, receiver_city, receiver_district, receiver_address,",
            "create_time, update_time",
            "FROM t_order",
            "WHERE receiver_phone_suffix = #{phoneSuffix}"
    })
    List<Order> findByReceiverPhoneSuffix(@Param("phoneSuffix") String phoneSuffix);
}